package cjlu.ea.h.controller;

import cjlu.ea.h.common.AuthUtil;
import cjlu.ea.h.common.R;
import cjlu.ea.h.common.TimeUtils;
import cjlu.ea.h.entity.OpTime;
import cjlu.ea.h.service.OpTimeService;
import cjlu.ea.h.service.SUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/h/auth")
public class AuthController {

    @Autowired
    OpTimeService opTimeService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    SUserService userService;

    @GetMapping("/getIsOptime/{Fname}")
    public R getOpTime(@PathVariable String Fname){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bz",Fname);
        OpTime opTime = opTimeService.getOne(queryWrapper);
        boolean isAva = TimeUtils.compareDate(opTime.getStartTime(), opTime.getEndTime());
        log.info("当前可操作状态:{}",isAva);
        return R.ok().data("isTime",isAva).data("startTime",opTime.getStartTime()).data("endTime",opTime.getEndTime());

    }
    @GetMapping("/getRole")
    public R getRole(HttpServletRequest request){
        AuthUtil authUtil = new AuthUtil();
        authUtil.setRedisTemplate(redisTemplate);
        String userRole =  authUtil.getAuthentications(request);
        log.info("获得用户角色为:{}",userRole);
        return R.ok().data("role",userRole);
    }
    @GetMapping("/getDepartment")
    public R getDepartment(HttpServletRequest request){
        String roles = userService.getUserRoles(userService.getToken(request));
        log.info("用户所属:{}",roles);
        return R.ok().data("department",roles);
    }

}
